import {
  getAppId
} from '@admin/utils/index';
import {
  usePageStore
} from '@/admin/store/modele/page';
import {
  apiRequest
} from '@/service/request';

export function getClientMacro() {
  const pageStore = usePageStore();
  const macro = pageStore.clientMacro;
  if (getAppId()) {
    macro.appId = getAppId();
  }
  const clientMacro = {
    clientMacro: JSON.stringify(macro)
  };
  return clientMacro;
}

// 获取页面配置信息
export function getPageInfo(query, options = {}) {
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/page/getPageInfo',
      method: 'get',
      params: query,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取子页面
export function getNavByPageId(data, options = {}) {
  const clientMacro = getClientMacro();
  data = {
    ...clientMacro,
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/init/getNavByPageId',
      method: 'post',
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取左侧区域数据
export function getLeftData(data, options = {}) {
  const clientMacro = getClientMacro();
  data = {
    ...clientMacro,
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/page/getLeftData',
      method: 'post',
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取主列表数据
export function getMainData(data, options = {}) {
  const clientMacro = getClientMacro();
  data = {
    ...clientMacro,
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/page/getMainData',
      method: 'post',
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 通用接口-单条数据
export function commonInvoke(data, clientMacro = false, options = {}) {
  let newData = JSON.parse(JSON.stringify(data));
  const appId = getAppId();
  newData = {
    appId,
    ...newData
  };
  if (clientMacro) {
    const clientMacro = getClientMacro();
    newData = {
      ...clientMacro,
      ...newData
    };
  }
  delete newData.data.REFDATA;
  delete newData.data.refdata;
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/invoke/func',
      method: 'post',
      data: newData,
      meta: {
        loading: true // 是否在接口请求时添加loading
      },
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 通用接口-多条数据
export function multiCommonInvoke(data, clientMacro = false, options = {}) {
  let newData = JSON.parse(JSON.stringify(data));
  newData.dataset.forEach(item => {
    delete item.REFDATA;
    delete item.refdata;
  });
  if (clientMacro) {
    const clientMacro = getClientMacro();
    newData = {
      ...clientMacro,
      ...newData
    };
  }
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/invoke/funcMoreData',
      method: 'post',
      data: newData,
      meta: {
        loading: true
      },
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 新增创建表单【废弃】
export function getAddFormInfo(data, options = {}) {
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/form/add',
      method: 'post',
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取详情数据【废弃】
export function getEditFormInfo(data, options = {}) {
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/form/getEditData',
      method: 'post',
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取表单详情
export function getFormData(data, options = {}) {
  const clientMacro = getClientMacro();
  data = {
    ...clientMacro,
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/form/getFormData',
      method: 'post',
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取表单详情
export function saveFormData(data, options = {}) {
  // 删除REFDATA字段
  const newData = JSON.parse(JSON.stringify(data));
  delete newData.data.REFDATA;
  delete newData.data.refdata;
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/form/saveFormData',
      method: 'post',
      data: newData,
      meta: {
        loading: true
      },
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取表单详情-手工
export function getFormDataFromId(data, options = {}) {
  const clientMacro = getClientMacro();
  data = {
    ...clientMacro,
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/form/getFormDataByFormId',
      method: 'post',
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取表单详情
export function saveFormDataByFormId(data, options = {}) {
  // 删除REFDATA字段
  const newData = JSON.parse(JSON.stringify(data));
  delete newData.data.REFDATA;
  delete newData.data.refdata;
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/form/saveFormDataByFormId',
      method: 'post',
      data: newData,
      meta: {
        loading: true
      },
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取菜单详情
export function invokeMethod(data, options = {}) {
  const newData = {
    pageId: (window.$CD && window.$CD.pageId) ? window.$CD.pageId : '',
    appId: getAppId(),
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/invoke/method',
      method: 'post',
      data: newData,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 多数据请求接口
export function invokeMethodMore(data, options = {}) {
  const newData = {
    pageId: (window.$CD && window.$CD.pageId) ? window.$CD.pageId : '',
    appId: getAppId(),
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/invoke/methodMoreData',
      method: 'post',
      data: newData,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取通用数据
export function getDataSource(data, options = {}) {
  const newData = {
    pageId: (window.$CD && window.$CD.pageId) ? window.$CD.pageId : '',
    appId: getAppId(),
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/invoke/datasource',
      method: 'post',
      data: newData,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取全部码表
export function getCodeData(data, options = {}) {
  const newData = {
    appId: getAppId() || '',
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/init/getCodeData',
      method: 'get',
      params: newData,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取lookup数据
export function getColLookupData(data, options = {}) {
  const clientMacro = getClientMacro();
  data = {
    pageId: window.$CD?.pageId || '',
    ...clientMacro,
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/form/getColLookupData',
      method: 'post',
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取系统服务数据
export function getColServiceData(data, options = {}) {
  const newData = {
    pageId: window.$CD?.pageId || '',
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/form/getColServiceData',
      method: 'post',
      data: newData,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 文件上传
export function fileUpload(data, file, options = {}) {
  const form = new FormData();
  Object.keys(data).forEach(key => {
    form.append(key, data[key]);
  });
  form.append('file', file);
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/file/upload',
      method: 'post',
      data: form,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 文件删除
export function fileDelete(params, options = {}) {
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/file/delete',
      method: 'get',
      params,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 保存排序
export function saveOrderId(data, options = {}) {
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/form/saveOrderId',
      method: 'post',
      data,
      meta: {
        loading: true
      },
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 保存自定义字段
export function saveDisplayList(data, options = {}) {
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/page/saveDisplayList',
      method: 'post',
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 保存自定义列宽
export function saveColumnWidth(data, options = {}) {
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/page/saveColumnWidth',
      method: 'post',
      data,
      meta: {
        globalWarn: false // 忽略报错
      },
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取宏变量
export function getMacro(data) {
  const clientMacro = getClientMacro();
  return clientMacro;
}

// 导出
export function tableExport(data, options = {}) {
  const clientMacro = getClientMacro();
  data = {
    ...clientMacro,
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/Export/download',
      method: 'post',
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 检测字段值是否唯一
export function checkColUnique(data, options = {}) {
  const newData = {
    pageId: window.$CD?.pageId || '',
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/form/checkColUnique',
      method: 'post',
      data: newData,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 复制-不入库
export function getCopyData(data, options = {}) {
  const clientMacro = getClientMacro();
  data = {
    ...clientMacro,
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/form/getCopyData',
      method: 'post',
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 统计
export function getStatisticInfoData(data, options = {}) {
  const clientMacro = getClientMacro();
  data = {
    ...clientMacro,
    ...data
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/page/getStatisticInfoData',
      method: 'post',
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取id
export function getOneId(query, options = {}) {
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/invoke/getOneId',
      method: 'get',
      params: query,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取人员
export function getEmployeeList(params, options = {}) {
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/connections/GetEmployeeList',
      method: 'get',
      params,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取通用数据
export function getExtendEleDisplayList(params, options = {}) {
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/page/getExtendEleDisplayList',
      method: 'get',
      params,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取列关联页面
export function getColRefPageInfo(params, options = {}) {
  const clientMacro = getClientMacro();
  const data = {
    ...clientMacro
  };
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/form/getColRefPageInfo',
      method: 'get',
      params,
      data,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 主表导入
export function importMainExcel(data, file, options = {}) {
  const form = new FormData();
  Object.keys(data).forEach(key => {
    form.append(key, data[key]);
  });
  form.append('file', file);
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/import/importMainTableData',
      method: 'post',
      data: form,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 子表导入
export function importSubExcel(data, file, options = {}) {
  const form = new FormData();
  Object.keys(data).forEach(key => {
    form.append(key, data[key]);
  });
  form.append('file', file);
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/import/importSubTableData',
      method: 'post',
      data: form,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取菜单信息
export function getNavInfo(params, options = {}) {
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/init/getNavInfo',
      method: 'get',
      params,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}

// 获取当前时间
export function getSystemDate(params, options = {}) {
  return new Promise((resolve, reject) => {
    apiRequest({
      url: '/user/getSystemDate',
      method: 'get',
      params,
      ...options
    }).then(res => {
      if (res.error) {
        reject(res.error.response.data);
      } else {
        resolve(res);
      }
    });
  });
}